package cn.outsourcing.supermarket.service.impl;

import cn.outsourcing.supermarket.common.constants.CommonConstants;
import cn.outsourcing.supermarket.common.exception.SupermarketException;
import cn.outsourcing.supermarket.common.utils.DataUtils;
import cn.outsourcing.supermarket.dto.OnlyIdPayloadDTO;
import cn.outsourcing.supermarket.dto.backstage.BackstageNoticeUpdateDTO;
import cn.outsourcing.supermarket.mapper.HiolabsNoticeMapper;
import cn.outsourcing.supermarket.po.HiolabsNotice;
import cn.outsourcing.supermarket.service.HiolabsNoticeService;
import cn.outsourcing.supermarket.vo.hiolabs.HiolabsNoticeVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author gzkemays
 * @since 2021-10-22
 */
@Service
public class HiolabsNoticeServiceImpl extends ServiceImpl<HiolabsNoticeMapper, HiolabsNotice>
    implements HiolabsNoticeService {
  @Resource private HiolabsNoticeMapper noticeMapper;

  @Override
  public List<HiolabsNoticeVO> getAllNotice() {
    List<HiolabsNotice> notices = noticeMapper.selectList(Wrappers.emptyWrapper());
    List<HiolabsNotice> toDelete = new ArrayList<>();
    for (HiolabsNotice notice : notices) {
      if (notice.getEndTime() < DataUtils.getSeconds()) {
        notice.setIsDelete(1);
        toDelete.add(notice);
      }
    }
    updateBatchById(toDelete);
    return notices.stream()
        .map(
            notice -> {
              HiolabsNoticeVO vo = new HiolabsNoticeVO();
              vo.setId(notice.getId());
              vo.setContent(notice.getContent());
              vo.setEndTime(DataUtils.getTimestampAllData(notice.getEndTime()));
              vo.setIsDelete(notice.getIsDelete());
              return vo;
            })
        .collect(Collectors.toList());
  }

  @Override
  public Integer updateNotice(BackstageNoticeUpdateDTO dto) {
    HiolabsNotice notice = getNoticePoByUpdateDto(dto, true);
    if (noticeMapper.updateById(notice) > 0) {
      return dto.getId();
    }
    throw new SupermarketException(CommonConstants.SQL_FAIL_CODE, "更新失败");
  }

  @Override
  public Integer addNotice(BackstageNoticeUpdateDTO dto) {
    HiolabsNotice notice = getNoticePoByUpdateDto(dto, false);
    if (noticeMapper.insert(notice) > 0) {
      return notice.getId();
    }
    throw new SupermarketException(CommonConstants.SQL_FAIL_CODE, "新增失败");
  }

  @Override
  public Integer deleteNotice(OnlyIdPayloadDTO dto) {
    HiolabsNotice notice = new HiolabsNotice();
    notice.setId(dto.getId());
    if (noticeMapper.deleteById(notice) > 0) {
      return dto.getId();
    }
    throw new SupermarketException(CommonConstants.SQL_FAIL_CODE, "删除失败");
  }

  /**
   * 根据入参 dto 转化为 po
   *
   * @param dto 入参对象
   * @param isUpdate 是否更新
   * @return po
   */
  private HiolabsNotice getNoticePoByUpdateDto(BackstageNoticeUpdateDTO dto, boolean isUpdate) {
    String time = dto.getTime();
    if (time.contains("T")) {
      time = time.replace("T", " ").replace("Z", "");
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    HiolabsNotice notice = new HiolabsNotice();
    if (isUpdate) {
      notice.setId(dto.getId());
    }
    notice.setContent(dto.getContent());
    try {
      notice.setEndTime(sdf.parse(time).getTime() / 1000);
    } catch (ParseException e) {
      e.printStackTrace();
    }

    return notice;
  }
}
